package chapter04

/**
 * @program lu-scala01 
 * @description: 不可变的数组
 * @author: zhanglu 
 * @create: 2022-03-29 15:23:00
 */
object Lu01_ImmutableArray {

  def main(args: Array[String]): Unit = {
    val arr: Array[Int] = new Array[Int](5)
    val arr2 = Array(1, 2, 3)

    println(arr)
    println(arr2)
    println(arr.length + "-" + arr2.length)
    println(arr(0) + "-" + arr2(0))

    arr(0) = 11
    println(arr(0))

    println("*****************")
    for(i <- 0 until arr2.length){
      print(arr2(i) + " ")
    }
    println("")

    println("*****************")
    for(i <- arr2.indices){
      print(arr2(i) + " ")
    }
    println("")

    println("********增强for循环*********")
    for (elem <- arr2){
      print(elem + " ")
    }
    println("")

    println("********迭代器*********")
    val iterator = arr2.iterator
    while (iterator.hasNext){
      print(iterator.next() + " ")
    }
    println("")

    println("********foreach*********")
    arr2.foreach(o => print(o + " "))
    println("")

    println("********foreach2*********")
    arr2.foreach(print)
    println("")

    println("*****************")
    println(arr2.mkString("-"))

    //向不可变数组后面添加元素
    val newArr2 = arr2.:+(4)
    println("*****************")
    println(newArr2.mkString("-"))

    //向不可变数组前面添加元素
    val newArr3 = arr2.+:(0)
    println("*****************")
    println(newArr3.mkString("-"))

    val newArr4 = arr2 :+ 4
    println("*****************")
    println(newArr4.mkString("-"))

    val newArr5 = 0 +: arr2 :+ 4 :+ 5
    println("*****************")
    println(newArr5.mkString("-"))


  }

}
